{include file=cms.library.3.dialog.tpl}
{*



This is no longer in use.
Instead the dialog is used, and modified to fit all uses.
This way we dont need to keep 2 allmost identical files up to date.




<h1>
    Bibliotek
</h1>
<div id="reportFrame"></div>
<h2>Billeder</h2>
<fieldset>
    <legend>Upload billeder</legend>
    <div id="fileQueue"></div>
    <br/>
    <input id="upload_button" type="file" name="files[]" />
</fieldset>
<script type="text/javascript" language="javascript">
    {literal}
    $(document).ready(function(){

        //Enable editor on textarea
        //$('#{/literal}{$identifier}content{literal}').markItUp(mySettings);

        $('#upload_button').uploadify({
            'uploader': 'modules/uploadify/uploadify.swf',
            'script': 'modules/uploadify/uploadify_.php',
            'folder': '/',
            'cancelImg': 'modules/uploadify/cancel.png',
            'queueID'        : 'fileQueue',
            'auto'           : true,
            'multi'          : true,
            'buttonText'    : 'Gennemse',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.png;*.gif',
            'simUploadLimit': 4,
            'scriptData':{
                'key':'IDDQD'
            },
            onSelectOnce: function (){
                busy(true);
            },
            onError: function (event, queueID ,fileObj, errorObj) {
                var msg;
                if (errorObj.status == 404) {
                    alert('Could not find upload script. Use a relative path.');
                    msg = 'Could not find upload script.';
                } else if (errorObj.type === "HTTP")
                    msg = errorObj.type+": "+errorObj.status;
                else if (errorObj.type ==="File Size")
                    msg = fileObj.name+'<br>'+errorObj.type+' Limit: '+Math.round(errorObj.sizeLimit/1024)+'KB';
                else
                    msg = errorObj.type+": "+errorObj.text;
                $.jGrowl('<p></p>'+msg, {
                    theme: 	'error',
                    header: 'ERROR',
                    sticky: true
                });
                $("#fileUploadgrowl" + queueID).fadeOut(250, function() { $("#fileUploadgrowl" + queueID).remove()});
                return false;
            },
            onCancel: function (a, b, c, d) {
                var msg = "Cancelled uploading: "+c.name;
                $.jGrowl('<p></p>'+msg, {
                    theme: 	'warning',
                    header: 'Cancelled Upload',
                    life:	3000,
                    sticky: false
                });
            },
            onClearQueue: function (a, b) {
                var msg = "Cleared "+b.fileCount+" files from queue";
                $.jGrowl('<p></p>'+msg, {
                    theme: 	'warning',
                    header: 'Cleared Queue',
                    life:	3000,
                    sticky: false
                });
            },
            onComplete: function (a, b ,c, d, e) {
                var size = Math.round(c.size/1024);

                switch (d){
                    case "PERM_ERR":
                        $.jGrowl('<p></p>Du har ikke tilladelse til at oprette nye billeder', {
                            theme: 	'error',
                            header: 'FEJL',
                            life:	2000,
                            sticky: false
                        });
                        break;
                    case "ERROR":
                        $.jGrowl('<p></p>Noget gik galt?!', {
                            theme: 	'error',
                            header: 'FEJL',
                            life:	2000,
                            sticky: false
                        });
                        break;
                    case "INVALID":
                        $.jGrowl('<p></p>Ikke accepteret filtype!', {
                            theme: 	'warning',
                            header: 'FEJL',
                            life:	3000,
                            sticky: false
                        });
                        break;
                    case "NOFILES":
                        $.jGrowl('<p></p>Der er ingen filer at uploade!', {
                            theme: 	'warning',
                            header: 'FEJL',
                            life:	1000,
                            sticky: false
                        });
                        break;
                    case "1":
                        $.jGrowl('<p></p>'+c.name+' - '+size+'KB', {
                            theme: 	'success',
                            header: 'Upload færdig',
                            life:	1000,
                            sticky: false
                        });
                        break;
                    default:
                        $.jGrowl('<p></p>'+d, {
                            theme: 	'warning',
                            header: 'Ukendt fejl',
                            life:	1000,
                            sticky: true
                        });

                    }

                },
                onAllComplete:function(){
                    refresh();
                    busy(false);
                }
            });

            $('#upload_queue').click(function(){
                $('#upload_button').uploadifyUpload();
            });
        });

        //yea {/literal}
</script>
<br/>
<div>
    <div id="libIndexHeader"><span>Bibliotek</span><span id="libraryPath">library:<span>#PATH NOT SET#</span></span></div>
    <div id="libIndexWrapper">
        <div id="libraryContent">
            <div class="libIndexContainerWrapper"><div class="libIndexContainerHeader"><span>Billeder</span></div>
                <div class="libIndexContainer" style="height:auto;">
                    <div id="imagelist">

                    </div>
                    <div class="clear"></div>
                </div>

            </div>
            <div class="libIndexContainerWrapper"><div class="libIndexContainerHeader"><span>Mapper</span> &nbsp;|&nbsp; <span id="addFolder" class="addButton" >Ny mappe</span></div>
                <div class="libIndexContainer"  style="height:auto;">
                    <div id="folderlist">


                    </div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
        <div id="libraryLoadingAnimation"></div>
    </div>
    <div class="clear"></div>
</div>


<script type="text/javascript">
        var lightboxImageUrl = "{$ALTURL}/modules/LightBox";
        var liburl = "{$LIBRARYURL}";
        var jsonUrl = "{$libraryCallUrl}";

        //and now the literal  {literal}

        var DEFAULT_THUMBNAIL_HEIGHT = 120;
        var DEFAULT_THUMBNAIL_WIDTH =160;

        var jsonData = {};
        var jsonCustomCallback = function(){};
        var folderName = "";
        var lastMouseMove = 0;


        function time(){
            var t = new Date();
            return Math.round(t.getTime());
        }


        $(function(){ // on dom ready
            $('#addFolder').click(addFolder);
            $(window).mousemove(function(){
                lastMouseMove = time();
            });

            refresh(); // refresh library
            lastMouseMove = time();
            autoRefresh();
        });

        function autoRefresh(){
            if (time() - lastMouseMove > 5000){
                refresh();
                lastMouseMove = time();
            }
            setTimeout(autoRefresh, 1000);
        }



        function refresh(){
            busy(true);
            refreshLibraryData(function(data){
                jsonData = data;
                updateIndex(function(){
                    attachImageEvents();
                    attachFolderEvents();
                    busy(false);
                    libraryLoadingAnimation(false);
                });
            });
        }

        function libraryLoadingAnimation(b){
            if(b){
                $("#libraryContent").hide();
                $("#libraryLoadingAnimation").show();
            }else{
                $("#libraryContent").show();
                $("#libraryLoadingAnimation").hide();
            }
        }


        function attachImageEvents(){
            $('a[rel*=lightbox]').lightBox({
                overlayBgColor: '#222',
                overlayOpacity: 0.5,
                imageLoading: lightboxImageUrl+'/images/lightbox-ico-loading.gif',
                imageBtnClose: lightboxImageUrl+'/images/lightbox-btn-close.gif',
                imageBtnPrev: lightboxImageUrl+'/images/lightbox-btn-prev.gif',
                imageBtnNext: lightboxImageUrl+'/images/lightbox-btn-next.gif',
                imageBlank: lightboxImageUrl+'/images/lightbox-blank.gif',
                containerResizeSpeed: 200,
                txtImage: 'Billede',
                txtOf: 'af',
                keyToPrev:",",
                keyToNext:"."
            });

            $("#imagelist .item").draggable({
                revert:"invalid",
                handle:".header",
                start:function(e,ui){
                    $(this).fadeTo(200,0.5);
                    $(this).addClass("drag_on_top");
                },
                stop:function(e,ui){
                    $(this).fadeTo(150,1);
                    $(this).removeClass("drag_on_top");
                }
            });
        }

        function attachFolderEvents(){
            $("#folderlist .item .thumbnail").click(function(){
                folderName = $(this).parent().attr('rel');
                $('#upload_button').uploadifySettings("folder",folderName);
                $('#imagelist').empty();
                $('#folderlist').empty();
                libraryLoadingAnimation(true);
                refresh();
            });

            $("#folderlist .item").droppable({
                accept:".item",
                hoverClass:"folderDropHover",
                drop:function(e,ui){;
                    moveFile(ui.draggable,$(this));
                }
            });

            $("#folderlist .moveable").draggable({
                revert:"invalid",
                handle:".header",
                zIndex:100,
                opacity:0.5
            });
        }


        function moveFile(eFile,eFolder){
            eFile.fadeTo(400,0.05);
            $.get("?q=command:do;action:library.file.move;ajax;file:"+eFile.attr('rel')+";to:"+eFolder.attr('rel')+";",function(data){
                if(data == "OK"){
                    eFile.hide();
                    $.jGrowl('<p></p>Billedet blev flyttet', {
                        theme: 	'success',
                        header: 'Flyttet',
                        life:	1000,
                        sticky: false
                    });
                }
                else{
                    $.jGrowl('<p></p>Kunne ikke flytte billedet - ' +data, {
                        theme: 	'error',
                        header: 'Fejl!',
                        life:	1000,
                        sticky: true
                    });
                }
                refresh();
            });
        }


        function createThumbnailUrl(url,height,width){
            if(!height)
                height = DEFAULT_THUMBNAIL_HEIGHT;
            if(!width)
                width = DEFAULT_THUMBNAIL_WIDTH;
            return liburl+"/image.php?height="+height+"&width="+width+"&image="+url;
        }
        function createImageUrl(url){
            return liburl+"/image.php?image="+url;
        }

        function updateIndex(callback){

            if(folderName != "")
                $("#libraryPath span").html(folderName);
            else
                $("#libraryPath span").html("/");

            // add images
            $('#imagelist').empty();
            var fragment = document.createDocumentFragment(); // create fragment to work much faster with the dom
            for(var i in jsonData.images){
                fragment.appendChild(createItem("image", jsonData.images[i], jsonData.images[i]));
            }
            document.getElementById("imagelist").appendChild(fragment); //append fragment to correct position

            // add folders
            $('#folderlist').empty();
            if(jsonData.images.length < 1)
            {
                $('#imagelist').html('<p style="text-align:center;">Der er ingen billeder i denne mappe.</p>');
            }

            var fragment = document.createDocumentFragment(); // create fragment to work much faster with the dom

            //create the back folder
            var fCheck = folderName.split("/");
            if(fCheck.length > 1){ // if you CAN go back
                fCheck = fCheck.slice(0,fCheck.length-1);
                var backUrl = fCheck.join("/");
                if(backUrl == "")
                    desc = "/";
                fragment.appendChild(createItem("folder",backUrl,desc,true));
            }

            for(var i in jsonData.folders){
                fragment.appendChild(createItem("folder", jsonData.folders[i],jsonData.folders[i]));
            }
            document.getElementById("folderlist").appendChild(fragment); //append fragment to correct position

            if(callback)
                callback();
        }

        function deleteImage($imageItem){
            if(confirm("Slet dette billede?")){
                $.get("?q=command:do;action:library.image.remove;ajax;image:"+$imageItem.attr("rel"),function(data){
                    if(data == "OK"){
                        $imageItem.hide(300,function(){
                            refresh();
                        });
                    }
                    else{
                        $.jGrowl('<p></p>Kunne ikke slette billedet', {
                            theme: 	'error',
                            header: 'Fejl!',
                            life:	1000,
                            sticky: false
                        });
                    }
                });
            }
        }

        function deleteFolder($folderItem){
            if(confirm("Er du sikker på at du vil slette denne mappe?\nAlle billeder og undermapper vil også blive slettet!")){
                $.get("?q=command:do;action:library.folder.remove;ajax;folder:"+$folderItem.attr('rel'),function(data){
                    if(data == "OK"){
                        $folderItem.hide(300,function(){
                            refresh();
                        });
                    }
                    else{
                        $.jGrowl('<p></p>Kunne ikke slette mappen', {
                            theme: 	'error',
                            header: 'Fejl!',
                            life:	1000,
                            sticky: false
                        });
                    }
                });
            }
        }

        function addFolder(){
            var name = prompt("Navn på ny mappe:");
            if(name){
                $.get("?q=command:do;action:library.folder.add;ajax;folder:"+folderName+"/"+name,function(data){
                    if(data == "OK")
                        refresh();
                    else{
                        $.jGrowl('<p></p>Kunne ikke oprette mappen', {
                            theme: 	'error',
                            header: 'Fejl!',
                            life:	1000,
                            sticky: false
                        });
                    }
                });
            }
        }

        function createItem(type,url,desc,secure){ // Create item


            var split = url.split("/");
            var filename = split[split.length-1];
            var descTxt = filename;

            var item = document.createElement("div");
            item.setAttribute("class","item");
            item.setAttribute("rel", url);

            var thumbnail = document.createElement("a");
            thumbnail.setAttribute("class","thumbnail");

            var description = document.createElement("div");
            description.setAttribute("class","description");

            var header = document.createElement("div");
            header.setAttribute("class","header");
            header.setAttribute("title", desc)
            header.appendChild(document.createTextNode(filename));

            item.appendChild(header);
            item.appendChild(thumbnail);
            item.appendChild(description);

            if(!secure){ // if not secured item, make deleteable
                var deleteBtn = document.createElement("span");
                deleteBtn.setAttribute("class", "deleteBtn");
                deleteBtn.setAttribute("rel",url);
                deleteBtn.appendChild(document.createTextNode("Slet"));

                description.appendChild(deleteBtn);

                $(item).addClass("moveable");
            }

            switch (type){
                case "image":
                    thumbnail.setAttribute("href", createImageUrl(url));
                    thumbnail.setAttribute("rel", "lightbox");
                    thumbnail.setAttribute("style", "background:#333 url('"+createThumbnailUrl(url)+"') no-repeat center center;");

                    if(!secure){
                        $(deleteBtn).click(function(e){
                            deleteImage($(this).parent().parent());
                            return false;
                        });
                    }
                    break;
                case "folder":
                    item.setAttribute("rel", url);
                    var thumbText = url.split("/");
                    thumbText = thumbText[thumbText.length-1];
                    var descText = thumbText;
                    if(secure){
                        var backTextLink = url;
                        if(url == "")
                            backTextLink = "/";
                        descText = "Tilbage til "+backTextLink;
                        thumbText = "Tilbage";


                        $(description).css({color:"#999"});
                        $(thumbnail).css({'background':"#333 url('style/folders/Generic.png') no-repeat center center"});
                    }else{
                        $(thumbnail).css({'background':"#333 url('style/folders/Generic.png') no-repeat center center"});
                    }
                    thumbnail.appendChild(document.createTextNode(thumbText));
                    if(!secure){
                        $(deleteBtn).click(function(e){
                            deleteFolder($(this).parent().parent());
                            return false;
                        });
                    }
                    break;
            }
            description.appendChild(document.createTextNode(descTxt));
            return item;
        }

        function refreshLibraryData(callback){
            $.getJSON(jsonUrl+folderName,function(data){
                callback(data);
            });
        }



        function busy(b){
            if(b){
                $('#busy_animation .s_ready').fadeOut(500);
            }else{
                $('#busy_animation .s_ready').fadeIn(500);
            }
        }
        //end of the literal  {/literal}
</script>
*}